package com.huike.web.controller.review;


import com.github.pagehelper.PageHelper;
import com.huike.common.core.controller.BaseController;
import com.huike.common.core.domain.AjaxResult;
import com.huike.common.core.page.TableDataInfo;
import com.huike.common.utils.DateUtils;
import com.huike.review.pojo.Review;
import com.huike.review.service.ReviewService;
import com.huike.review.vo.MybatisReviewVO;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 该Controller主要是为了复习三层架构以及Mybatis使用的，该部分接口已经放开权限，可以直接访问
 * 同学们在此处编写接口通过浏览器访问看是否能完成最简单的增删改查
 */
@RestController
@RequestMapping("/review")
public class MybatisReviewController extends BaseController {

    @Autowired
    private ReviewService reviewService;


    @GetMapping("/saveData/{name}/{age}/{sex}")
    public AjaxResult saveData(@PathVariable String name,@PathVariable Integer age,@PathVariable String sex){
//        reviewService.saveData(name,age,sex);
        Review review = new Review();
        review.setName(name);
        review.setAge(age);
        review.setSex(sex);
        reviewService.saveData(review);
        return AjaxResult.success("成功插入:1条数据",null);


    }

    /**=========================================================新增数据============================================*/

    @PostMapping("/saveData")
    public AjaxResult add(@RequestBody Review review){
        reviewService.add(review);
        return AjaxResult.success("成功插入:1条数据",null);
    }
    /**=========================================================删除数据=============================================*/
    @DeleteMapping("/remove/{id}")
    public AjaxResult remove(@PathVariable Long id){
        reviewService.removeById(id);
        return AjaxResult.success("删除成功",null);
    }
    /**=========================================================修改数据=============================================*/

    @PostMapping("/update")
    public AjaxResult updated(@RequestBody Review review){
        reviewService.updated(review);
        return AjaxResult.success("修改成功",null);
    }
    /**=========================================================查询数据=============================================*/

    @GetMapping("/getById")
    public AjaxResult getById(Long id){
        Review review = reviewService.getById(id);
        return AjaxResult.success(review);
    }
    @GetMapping("getDataByPage")
    public TableDataInfo getDataByPage(@RequestParam("page") Integer page,
                                       @RequestParam("pageSize") Integer pageSize){
        List<MybatisReviewVO> list = reviewService.getDataByPage(page,pageSize);
        //PageHelper.startPage(page, pageSize);分页插件
        return getDataTablePage(list);
    }

}